# -*- coding: utf - 8 - *-
import os
import sys
import threads
import time
import random
from lxml import etree

from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtGui import QPixmap
from DB_OPERTER import DB

from win_2048 import Ui_MainWindow

from TrayIcon import TrayIcon

import requests  # 先导入爬虫的库，不然调用不了爬虫的函数
import json

import datetime
from openpyxl import load_workbook

import pygame


class MED_SELL(QMainWindow, Ui_MainWindow):
    _signal = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        # self.setWindowFlags(Qt.FramelessWindowHint)
        self.retranslateUi(self)

        self.Connections()
        self.DB = DB()
        self.a = [i for i in range(15)]
        self.texts = ["太原", "大同", "阳泉", "长治", "晋城", "朔州", "忻州", "晋中", "临汾", "运城", "吕梁", "省直", "高校", "国资", "融办"]
        self.widgets = [
            self.label_1,
            self.label_2,
            self.label_3,
            self.label_4,
            self.label_5,
            self.label_6,
            self.label_7,
            self.label_8,
            self.label_9,
            self.label_10,
            self.label_11,
            self.label_12,
            self.label_13,
            self.label_14,
            self.label_15,
        ]

        self.thread_Function_Split = threads.Browser()
        self.thread_Function_Split.list_idx = self.a
        self.thread_Function_Split.label_signal.connect(self.set_it)

        self.last = 0

    def Connections(self):
        self.pushButton.clicked.connect(self.chou_qian)
        pass

# -------------------------------------------------------------------------------------------------------------------
    def set_12(self, idx, text):

        self.widgets[idx].setStyleSheet("background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);")
# -------------------------------------------------------------------------------------------------------------------

    def set_36(self, idx, text):

        if text == "这是最后一家":
            for i in range(15):
                self.widgets[i].setStyleSheet("background-color: rgb(0, 0, 0);color: rgb(255, 255, 255);")

            pygame.init()
            pygame.mixer.music.load("./2.mp3")
            pygame.mixer.music.set_volume(1)
            for i in range(3):
                pygame.mixer.music.play()
                time.sleep(1)
                pygame.mixer.music.pause()
            return
        else:
            self.widgets[idx].setStyleSheet("background-color: rgb(255, 0, 0);color: rgb(255, 255, 255);")
            pygame.mixer.music.pause()
            pygame.init()
            pygame.mixer.music.load("./2.mp3")
            pygame.mixer.music.set_volume(1)
            pygame.mixer.music.play()
            # time.sleep(1.5)

    def chou_qian(self):
        if self.a == []:
            self.set_36(1, "这是最后一家")
        else:
            self.progressBar.setValue(100)
            self.thread_Function_Split.start()
            pygame.init()
            pygame.mixer.music.load("./1.mp3")
            pygame.mixer.music.set_volume(1)
            pygame.mixer.music.play()

    def set_it(self, idx, idx2, text):
        for wid in self.widgets:
            wid.setStyleSheet("background-color: rgb(255, 255, 255);color: rgb(0, 0, 0);")

        self.progressBar.setValue(idx2 * 20)
        if text == "pass":

            time.sleep(0.7)
            self.set_12(idx, self.texts[idx])
        else:

            self.set_36(self.a[idx], self.texts[self.a[idx]])
            self.a.pop(idx)
            self.thread_Function_Split.list_idx = self.a
            print("self.avvv ", self.a)

        # random.shuffle(self.a)
        # print("new_list", self.a)
        # for i in range(self.a.__len__()):
        #     time.sleep(0.2)
        #     self.set_12(self.texts[i])
        #     if i == self.a[-1]:
        #         self.set_36(self.texts[i])
        #         self.a.pop(-1)

    def echo(self, *args):
        print(datetime.datetime.now())
        print([str(i) for i in args])

    def Exit(self):
        app.quit()

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key() == Qt.Key_Escape:
            self.tray.onIconClicked(3)
        elif QKeyEvent.key() == Qt.Key_Return:
            self.List_Refresh()

    def minimized(self):
        self.tray.onIconClicked(2)

    def mousePressEvent(self, event):  # 重写鼠标点击事件
        if event.button() == QtCore.Qt.LeftButton:
            self.Move = True  # 鼠标按下时设置为移动状态
            self.Point = event.globalPos() - self.pos()  # 记录起始点坐标
            event.accept()

    def mouseMoveEvent(self, QMouseEvent):  # 移动时间
        if self.isMaximized() == 0:
            if QtCore.Qt.LeftButton and self.Move == False:  # 切记这里的条件不能写死，只要判断move和鼠标执行即可！
                self.move(QMouseEvent.globalPos() - self.Point)  # 移动到鼠标到达的坐标点！
                QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):  # 结束事件
        self.Move = False


if __name__ == '__main__':
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    main_window = MED_SELL()
    main_window.show()
    # main_window.showMaximized()
    sys.exit(app.exec_())
